home *** CD-ROM | disk | FTP | other *** search
- //------------------------------------------------------------------------------------------------------
- // Name : import_.c
- // Date : 23.05.1996 Author : SM System : Win32
- //------------------------------------------------------------------------------------------------------
- // This file contains the language-independent implementation of the module IMPORT_.DLL. All texts and
- // resources that are language-dependent are located in an additional IMPORT.DLL, whose sources can be
- // found in the subdirectories \E (for English) and \D (for German).
- //------------------------------------------------------------------------------------------------------
-
- #define USER_DATA_ID "1.00-1995-08-10"
-
- //------------------------------------------------------------------------------------------------------
-
- #include "windows.h"
- #include "windowsx.h"
- #include "stdlib.h"
- #include "stdio.h"
- #include "math.h"
-
- #include "e:\release4\toso40.h" // Toso Interface 4.0 Definitions
- #include "dialog.h"
-
- //------------------------------------------------------------------------------------------------------
-
- typedef struct {
- STR32 TimeStamp;
-
- FILENAME FileName;
- } INF_HEADER;
-
- //------ Language-dependent texts in IMPORT.DLL --------------------------------------------------------
-
- DLL_IMPORT LPSTR
- eStartUpText [],
- eDefaultName [],
- eDialogText [],
- eMessageText [];
-
- //------------------------------------------------------------------------------------------------------
-
- static HINSTANCE hInstDLL, // Instance handle of the main DLL
- hLanguage, // Instance handle of the language DLL
- hGlobalInst; // Instance handle of the serving application
- static HWND hGlobalWnd; // Main window handle of the serving application
-
- //------------------------------------------------------------------------------------------------------
-
- static HBITMAP hBitmap;
- static INF_HEADER INFHeader;
- static int gError; // Current error status ( 0 = OK )
-
- //------------------------------------------------------------------------------------------------------
-
- BOOL ModuleLoadSettings( void )
- {
- BOOL Result = FALSE;
-
- if( TosoProfileReadKeyOpen( "IMPORT", FALSE ) ) {
- if( TosoProfileReadData( "Init", (LPBYTE) &INFHeader, sizeof( INFHeader ) ) )
- Result = TRUE;
- TosoProfileReadKeyClose();
-
- INFHeader.TimeStamp[31] = 0x00;
- if( lstrcmp( INFHeader.TimeStamp, USER_DATA_ID ) )
- Result = FALSE;
- }
-
- if( !Result ) {
- lstrcpy( INFHeader.FileName, eDefaultName[0] );
- }
- return( Result );
- }
-
- //------------------------------------------------------------------------------------------------------
-
- BOOL ModuleSaveSettings( void )
- {
- BOOL Result = FALSE;
-
- if( TosoProfileWriteKeyOpen( "IMPORT", FALSE ) ) {
- lstrcpy( INFHeader.TimeStamp, USER_DATA_ID );
- if( TosoProfileWriteData( "Init", (LPBYTE) &INFHeader, sizeof( INFHeader ) ) )
- Result = TRUE;
- TosoProfileWriteKeyClose();
- }
- return( Result );
- }
-
- //------------------------------------------------------------------------------------------------------
- // This procedure reads a single coordinate pair from the current file. If it finds the last line of the
- // file (indicated by the DB_END data block identifier), it returns FALSE without setting gError to a
- // non-zero value.
-
- BOOL ModuleReadCoordinate( double* x, double* y )
- {
- DUMMYSTR Text1, Text2;
- short Dummy;
-
- if( gError )
- return( FALSE );
-
- wsprintf( Text1, eDialogText[3], TosoFileReadCurrentLine() );
- wsprintf( Text2, eDialogText[4], ( TosoFileReadCurrentSize() + 1023 ) / 1024 );
- TosoDialogUpdateProgress( Text1, Text2, TosoFileReadCurrentSize(), TosoFileReadTotalSize() );
-
- if( TosoDialogIsCanceled() ) {
- gError = 999;
- return( FALSE );
- }
-
- TosoFileReadShort( &Dummy );
- if( Dummy == DB_END )
- return( FALSE );
-
- TosoFileReadCommaDouble( x );
- TosoFileReadCommaDouble( y );
- TosoFileReadSemi();
-
- if( TosoFileReadError() ) {
- gError = 2;
- return( FALSE );
- }
-
- return( TRUE );
- }
-
- //------------------------------------------------------------------------------------------------------
- // This procedure performs the import. It reads all coordinate pairs from the import file and insert a
- // marking for each coordinate pair.
- // In addition, it initializes and display a progress indicator window to inform the user about the
- // current progress and to allow him to cancel the import. Since the final file size is already known
- // in advance, the progress indicator does include a percent bar.
-
- void ModuleImport( HANDLE FileHandle, const LPSTR FileName )
- {
- FILENAME FileName2;
- DUMMYSTR DummyStr;
- int Count;
- double x, y;
-
- gError = 0;
-
- if( !TosoFileReadInitDisk( FileHandle ) )
- return;
-
- TosoFileSplitName( FileName, NULL, FileName2 );
- wsprintf( DummyStr, eDialogText[2], FileName2 );
- TosoDialogShowProgress( eDialogText[0], DummyStr, TRUE );
-
- if( ModuleReadCoordinate( &x, &y ) ) {
- Count = 0;
-
- do {
- if( Count == 0 )
- TosoObjectOpen( OBJ_MARK );
-
- TosoObjectAddPoint( DB_POINT_MARK, x, y );
- Count++;
-
- if( Count >= POINTS_PER_OBJECT ) {
- if( !TosoObjectFastInsert() ) {
- gError = 998;
- goto _stop;
- }
- Count = 0;
- }
- } while( ModuleReadCoordinate( &x, &y ) );
-
- if( Count > 0 )
- if( !TosoObjectFastInsert() )
- gError = 998;
-
- TosoFileReadSemi();
- }
-
- _stop:
- TosoFileReadExit();
- TosoDialogHideProgress();
- }
-
- //------------------------------------------------------------------------------------------------------
- // This DLL entry procedure must exist in any DLL to be used in Win32. Since our DLL does all necessary
- // initialization in its TosoModuleInit() procedure, this procedure is quite empty.
-
- BOOL WINAPI DllMain( HINSTANCE hInstance, DWORD Reason, LPVOID Dummy )
- {
- switch( Reason ) {
- case DLL_PROCESS_ATTACH:
- hInstDLL = hInstance;
- break;
-
- case DLL_PROCESS_DETACH:
- hInstDLL = NULL;
- break;
- }
- return( TRUE );
- }
-
- //------------------------------------------------------------------------------------------------------
- // This procedure is called when the module is loaded by the serving application. Its main tasks are:
- // - Checking whether it is compatible with the given InterfaceVersion
- // - Checking whether it is licensed to the given serial number (optional)
- // - Storing of the serving application's instance and main windows handle for further use
- // - Loading of the language-dependent library
- // - Filling in the module ID structure whose address is passed in ModuleID
- // - Loading of options from the registry database
- // - Loading profiles
- // - Allocating any static memory required
-
- DLL_EXPORT BOOL TosoModuleInit( const LPSTR SerialNumber, HINSTANCE hMainInst, HWND hMainWnd,
- int InterfaceVersion, MODULE_ID* ModuleID )
- {
- if( InterfaceVersion < TOSO_INTERFACE_VERSION ) {
- MessageBox( hMainWnd, eMessageText[0], eDialogText[0], MB_OK );
- return( FALSE );
- }
- hGlobalInst = hMainInst;
- hGlobalWnd = hMainWnd;
-
- hLanguage = LoadLibrary( "IMPORT.DLL" );
- hBitmap = LoadBitmap( hLanguage, "IDB_COMMAND" );
-
- ModuleID->OwnerID = DB_OWNER_TOSO;
- ModuleID->ModuleID = 0x1000;
- ModuleID->ModuleCTRL = MODULECTRL_ALL;
-
- ModuleID->ModuleProc.InputPointInitProc = (TOSOINPUTPOINTINIT_PROC) NULL;
- ModuleID->ModuleProc.InputPointMoveProc = (TOSOINPUTPOINTMOVE_PROC) NULL;
- ModuleID->ModuleProc.InputPointExitProc = (TOSOINPUTPOINTEXIT_PROC) NULL;
- ModuleID->ModuleProc.InputDisplayProc = (TOSOINPUTDISPLAY_PROC) NULL;
- ModuleID->ModuleProc.InputParameterProc = (TOSOINPUTPARAMETER_PROC) NULL;
- ModuleID->ModuleProc.InputCancelProc = (TOSOINPUTCANCEL_PROC) NULL;
- ModuleID->ModuleProc.InputFinishProc = (TOSOINPUTFINISH_PROC) NULL;
-
- ModuleID->ModuleData.Type = MODULETYPE_IMPORT;
-
- ModuleID->ModuleData.InputData.CommandMode = COMMAND_DIRECT;
- ModuleID->ModuleData.MenuData.MenuEntry = eStartUpText[1];
- ModuleID->ModuleData.MenuData.Description = eStartUpText[2];
-
- ModuleID->ModuleData.IconHandle = hBitmap;
- ModuleID->ModuleData.IconXOffset = 0;
- ModuleID->ModuleData.IconYOffset = 0;
- ModuleID->ModuleData.IconMode = 0;
-
- ModuleID->CommandData = NULL;
-
- ModuleLoadSettings();
-
- return( TRUE );
- }
-
- //------------------------------------------------------------------------------------------------------
- // This procedure is called when the module is removed by the serving application. Its main tasks are:
- // - Checking whether anything is to be saved. If so, it should display a message information the user
- // about it and allowing him to save those changes.
- // - Freeing of all statically allocated memory.
- // If this procedure return FALSE, the serving application will not be able to terminate. So please, do
- // only return FALSE if shutting down the module now would severely damage or destroy user data.
-
- DLL_EXPORT BOOL TosoModuleExit( void )
- {
- ModuleSaveSettings();
-
- DeleteBitmap( hBitmap );
-
- return( TRUE );
- }
-
- //------------------------------------------------------------------------------------------------------
- // This procedure serves as hook procedure for the comman file open dialog window. It manages the Infos
- // button that has been added to the standard GetOpenFileName dialog template.
- // In a more sophisticated import module, the common dialog would also contain a Options button which
- // would also be managed in this procedure.
-
- UINT CALLBACK TosoModuleGetFileNameHook( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam )
- {
- switch( message ) {
- case WM_INITDIALOG: // Initialize dialog box
- TosoDialogCenter( hDlg ); // Center dialog window
- return( 1 );
-
- case WM_COMMAND:
- switch( GET_WM_COMMAND_ID( wParam, lParam ) ) {
- case IDD_BUTTON0: // Infos
- MessageBox( hDlg, eStartUpText[0], eDialogText[0], MB_OK );
- return( 1 );
- }
- break;
-
- case WM_ENTERIDLE:
- return( TosoDialogEnterIdle( hDlg, wParam, lParam ) );
-
- default:
- if( message == TosoDialogHelpMessage() ) {
- WinHelp( hDlg, "IMPORT.HLP", HELP_CONTEXT, 1 );
- return( 1 );
- }
- break;
- }
- return( 0 ); // Didn't process a message
- }
-
- //------------------------------------------------------------------------------------------------------
- // This procedure is called when a module's command is chosen by the user. For an import filter, its
- // main tasks are:
- // - Prompting the user for the file name of the import file. This should usually be done by means of
- // common dialog windows extended by some additional buttons like Infos and Options
- // - Opening the import file
- // - Calling the basic import procedure
- // - Closing the import file
- // - Error handling and display
-
- DLL_EXPORT BOOL TosoModuleCommand( int CommandID, int ExecMode )
- {
- static OPENFILENAME OpenData;
- static FILENAME FileName;
- HANDLE FileHandle;
- DUMMYSTR DummyStr;
- DWORD CmnDlgError;
- BOOL Result = FALSE;
-
- if( CommandID != 0 )
- return( FALSE );
-
- // Check whether a help topic is to be displayed instead of starting a command.
-
- if( ExecMode == MODULEEXEC_HELP ) {
- WinHelp( hGlobalWnd, "IMPORT.HLP", HELP_CONTEXT, 1 );
- return( FALSE );
- }
-
- if( !TosoCreationStart() )
- return( FALSE );
-
- lstrcpy( FileName, INFHeader.FileName );
-
- OpenData.lStructSize = sizeof( OPENFILENAME );
- OpenData.hwndOwner = hGlobalWnd;
- OpenData.hInstance = hLanguage;
- OpenData.lpstrFilter = eDialogText[1];
- OpenData.lpstrCustomFilter = NULL;
- OpenData.nMaxCustFilter = 0;
- OpenData.nFilterIndex = 1;
- OpenData.lpstrFile = FileName;
- OpenData.nMaxFile = sizeof( FileName );
- OpenData.lpstrFileTitle = NULL;
- OpenData.nMaxFileTitle = 0;
- OpenData.lpstrInitialDir = NULL;
- OpenData.lpstrTitle = eDialogText[0];
- OpenData.Flags = OFN_HIDEREADONLY | OFN_NONETWORKBUTTON | OFN_SHOWHELP |
- OFN_ENABLEHOOK | OFN_ENABLETEMPLATE | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
- OpenData.nFileOffset = 0;
- OpenData.nFileExtension = 0;
- OpenData.lpstrDefExt = eDefaultName[2];
- OpenData.lCustData = NOPARAM;
- OpenData.lpfnHook = (LPOFNHOOKPROC) TosoModuleGetFileNameHook;
- OpenData.lpTemplateName = "GETFILENAME";
-
- Result = GetOpenFileName( &OpenData ); // Open common dialog window
-
- if( !Result ) // TRUE=OK, FALSE=cancel or error
- CmnDlgError = CommDlgExtendedError(); // 0=cancel, else error number
-
- if( !Result ) { // Cancel
- if( CmnDlgError ) { // Error in common dialog
- wsprintf( DummyStr, eMessageText[3], CmnDlgError );
- MessageBox( hGlobalWnd, DummyStr, eDialogText[0], MB_OK );
- }
- return( TRUE );
- }
- else // Store the selected file name
- lstrcpy( INFHeader.FileName, FileName );
-
- Result = FALSE;
- SetCursor( LoadCursor( NULL, IDC_WAIT ) );
-
- if( TosoFileOpen( &FileHandle, FileName ) ) {
- TosoUndoInitProcess();
-
- ModuleImport( FileHandle, FileName );
-
- TosoFileClose( FileHandle );
- switch( gError ) {
- case 999:
- TosoUndoCancelProcess();
- MessageBox( hGlobalWnd, eMessageText[2], eDialogText[0], MB_OK );
- Result = TRUE;
- break;
-
- case 998:
- TosoUndoCancelProcess();
- MessageBox( hGlobalWnd, eMessageText[4], eDialogText[0], MB_OK );
- Result = FALSE;
- break;
-
- case 0:
- TosoUndoFinishProcess();
- // TosoUndoUpdateLinks(); <- Would only be necessary if blocks or instances had been modified!
- TosoDrawWindowAll();
- Result = TRUE;
- break;
-
- default:
- TosoUndoCancelProcess();
- wsprintf( DummyStr, eMessageText[5], gError, TosoFileReadCurrentLine(), TosoFileReadCurrentSize() );
- MessageBox( hGlobalWnd, DummyStr, eDialogText[0], MB_OK );
- Result = FALSE;
- break;
- }
- }
- TosoCreationEnd();
- return( Result );
- }
-